﻿using System;
using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_mapping_between_arrays_with_nested_types
    {
        public class DomainPerson { public DomainAddress[] Addresses { get; set; } }
        public class DomainAddress { public String City { get; set; } }

        public class GuiPerson { public GuiAddress[] Addresses { get; set; } }
        public class GuiAddress { public String City { get; set; } }

        private readonly Mapping<DomainPerson, GuiPerson> mapping;

        public When_mapping_between_arrays_with_nested_types()
        {
            mapping = new Mapping<DomainPerson, GuiPerson>();
            var addressMapping = new Mapping<DomainAddress, GuiAddress>(x=>new DomainAddress(),x=>new GuiAddress());
            addressMapping.AutoRelateEqualNames();
            mapping.Relate(domain=>domain.Addresses,gui=>gui.Addresses,addressMapping);
        }

        [Fact]
        public void Should_map_correctly_TowardsRight()
        {
            var domainPerson = new DomainPerson
               {
                   Addresses = new DomainAddress[]
                           {
                               new DomainAddress {City = "City1"}, 
                               new DomainAddress {City = "City2"}
                           }
               };

            var guiPerson = mapping.Map(domainPerson, new GuiPerson());
            Assert.Equal(domainPerson.Addresses.Length, guiPerson.Addresses.Length);
            Assert.Equal(domainPerson.Addresses[0].City,guiPerson.Addresses[0].City);
            Assert.Equal(domainPerson.Addresses[1].City, guiPerson.Addresses[1].City);
        }

        [Fact]
        public void Should_map_correctly_TowardsLeft()
        {
            var guiPerson = new GuiPerson
            {
                Addresses = new GuiAddress[]
                           {
                               new GuiAddress {City = "City1"}, 
                               new GuiAddress {City = "City2"}
                           }
            };

            var domainPerson = mapping.Map(guiPerson, new DomainPerson());
            Assert.Equal(domainPerson.Addresses.Length, guiPerson.Addresses.Length);
            Assert.Equal(domainPerson.Addresses[0].City, guiPerson.Addresses[0].City);
            Assert.Equal(domainPerson.Addresses[1].City, guiPerson.Addresses[1].City);
        }

        [Fact]
        public void Should_not_map_nulls_towardsRight()
        {
            var domainPerson = new DomainPerson {Addresses = null};
            var guiPerson = mapping.Map(domainPerson, new GuiPerson());
            Assert.Null(guiPerson.Addresses);
        }

        [Fact]
        public void Should_not_map_nulls_towardsLeft()
        {
            var guiPerson = new GuiPerson { Addresses = null };
            var domainPerson = mapping.Map(guiPerson, new DomainPerson());
            Assert.Null(domainPerson.Addresses);
        }
    }
}
